package com.ltsznh.android.bluecompass;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;

/**
 * Created by ltsznh on 13-12-12.
 */
class CompassThread extends Thread{

    private final SurfaceHolder compassSH;
    private Paint cPaint = new Paint();
    private Canvas canvas;

    private int cWidth = 0;
    private int cHeight = 0;
    private float cX = 0;
    private float cY = 0;
    private float cR = 0;
    private double zR = 0.85;

    public CompassThread(SurfaceHolder surfaceHolder) {
        // TODO Auto-generated constructor stub
        compassSH = surfaceHolder;

        cPaint.setColor(Color.rgb(0, 153, 204));
        cPaint.setAntiAlias(true);//锯齿效果


    }

    protected void setSize(int x,int y){
        cWidth = x;
        cHeight = y;

        cX = cWidth/2;
        cY = cHeight/2;
        cR = Math.min(cX,cY) * 9/10;
    }

    public void run(){
        canvas = compassSH.lockCanvas();

        try {
            synchronized (compassSH) {

                drawCompass();//画表盘

        }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (canvas != null) {
                compassSH.unlockCanvasAndPost(canvas);
            }
        }
    }

    public void drawCompass(){
        double angle = MainActivity.angle;

//                画圆圈
        canvas.drawCircle(cX,cY,cR,CompassView.clearPaint);//清空表盘
        canvas.drawRect(20,60,cX-cR,cY-cR,CompassView.clearPaint);//清空表盘

        cPaint.setStyle(Paint.Style.STROKE);//设置空心
        cPaint.setStrokeWidth(6);//线条粗细
        canvas.drawCircle(cX,cY,cR,cPaint);
        cPaint.setStyle(Paint.Style.FILL);//设置实心
        canvas.drawCircle(cX,cY,10,cPaint);//画中心

//                画刻度
        cPaint.setStrokeWidth(6);
        cPaint.setTextSize(18);
        for(double i = 0;i < 360;i+=90){
            drawGraduation(angle, i,zR);
            if(i == 0){
                canvas.drawText("S",(float) (cX + Math.sin((angle + i) / 180 * Math.PI) * cR * zR * 0.95),
                        (float) (cY + Math.cos((angle + i) / 180 * Math.PI) * cR * zR * 0.95),
                        cPaint);
            }else if(i == 90){
                canvas.drawText("E",(float) (cX + Math.sin((angle + i) / 180 * Math.PI) * cR * zR * 0.95),
                        (float) (cY + Math.cos((angle + i) / 180 * Math.PI) * cR * zR * 0.95),
                        cPaint);
            }else if(i == 180){
                canvas.drawText("N",(float) (cX + Math.sin((angle + i) / 180 * Math.PI) * cR * zR * 0.95),
                        (float) (cY + Math.cos((angle + i) / 180 * Math.PI) * cR * zR * 0.95),
                        cPaint);
            }else if(i == 270){
                canvas.drawText("W",(float) (cX + Math.sin((angle + i) / 180 * Math.PI) * cR * zR * 0.95),
                        (float) (cY + Math.cos((angle + i) / 180 * Math.PI) * cR * zR * 0.95),
                        cPaint);
            }

        }

        cPaint.setStrokeWidth(3);
        for(double i = 30;i < 360;i+=30){
            if(i%90 > 0){
                drawGraduation(angle, i,0.88);
            }
        }

        cPaint.setStrokeWidth(1);
        for(double i = 10;i < 360;i+=10){
            if(i%30 > 0){
                drawGraduation(angle, i,0.92);
            }
        }

        //                画指针
        cPaint.setStrokeWidth(2);
        canvas.drawLine(cX,(float)(cY + cR * zR),cX,(float)(cY - cR * zR),cPaint);
        canvas.drawLine((float)(cX - cR * zR),cY,(float)(cX + cR * zR),cY,cPaint);

//        画GPS方向指针
        if(MainActivity.GPSAngle >0 || !MainActivity.isExpired){
            cPaint.setStrokeWidth(5);
            if(MainActivity.isExpired){
                cPaint.setColor(Color.GRAY);
            }else {
                cPaint.setColor(Color.rgb(0, 153, 204));
            }
            canvas.drawLine((float) (cX),
                    (float) (cY),
                    (float) (cX + Math.sin(( MainActivity.GPSAngle - angle) / 180 * Math.PI) * cR),
                    (float) (cY - Math.cos(( MainActivity.GPSAngle - angle) / 180 * Math.PI) * cR),
                    cPaint);
        }
        cPaint.setColor(Color.rgb(0, 153, 204));

        //        画压强
        if(MainActivity.Pressure >= 0){
            canvas.drawRect(20, 60, 200, 100, CompassView.clearPaint);//清空表盘
            cPaint.setTextSize(26);
            canvas.drawText(MainActivity.Pressure + "PA",20,60,cPaint);
        }


    }

    public void drawGraduation(double angle,double angleGraduation,double zR){
        canvas.drawLine((float) (cX + Math.sin((angle + angleGraduation) / 180 * Math.PI) * cR * zR),
                (float) (cY + Math.cos((angle + angleGraduation) / 180 * Math.PI) * cR * zR),
                (float) (cX + Math.sin((angle + angleGraduation) / 180 * Math.PI) * cR),
                (float) (cY + Math.cos((angle + angleGraduation) / 180 * Math.PI) * cR),
                cPaint);
    }
}